package com.ruoyi.business.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.business.MyPage;
import com.ruoyi.business.PageConfig;
import com.ruoyi.business.RechargeActivity;
import com.ruoyi.business.User;
import com.ruoyi.business.ao.RechargeActivityAO;
import com.ruoyi.business.enums.ActivityStatusEnum;
import com.ruoyi.business.mapper.RechargeActivityMapper;
import com.ruoyi.business.mapper.UserMapper;
import com.ruoyi.business.vo.RechargeActivityListVO;
import com.ruoyi.common.common.CommonEnum;
import com.ruoyi.common.common.ResultBody;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;


/**
 * @author ：jiangguanjun
 * @description：充值活动
 * @date ：2020/7/16 21:13
 */
@Slf4j
@Component
public class RechargeActivityService  {

    @Autowired
    private RechargeActivityMapper rechargeActivityMapper ;
    @Autowired
    private UserMapper userMapper ;

    public ResultBody add(RechargeActivityAO rechargeActivityAO) {

        RechargeActivity rechargeActivity = new RechargeActivity() ;
        rechargeActivity.setGiveMoney(rechargeActivityAO.getGiveMoney());
        rechargeActivity.setCreateBy(111);
        rechargeActivity.setMoney(rechargeActivityAO.getMoney());
        rechargeActivity.setuId(rechargeActivityAO.getuId());
        rechargeActivity.setStartTime(new Date(rechargeActivityAO.getStartTime()));
        rechargeActivity.setEndTime(new Date(rechargeActivityAO.getEndTime()));
        rechargeActivity.setName(rechargeActivityAO.getName());

        int res = rechargeActivityMapper.insertSelective(rechargeActivity) ;
        if(res>0){
            return ResultBody.success() ;
        }
        return ResultBody.error(CommonEnum.RECHARGE_ACTIVITY_SAVE_ERROR) ;
    }

    public ResultBody<IPage<RechargeActivityListVO>> list(Integer uId, Integer status) {
        Integer adminId = 111 ;

        MyPage mypage = PageConfig.getPage() ;
        Page<RechargeActivityListVO> page = new Page<>(mypage.getPageNum(), mypage.getPageSize()) ;

        IPage<RechargeActivityListVO> iPage = rechargeActivityMapper.listByPage(page,uId,status,adminId) ;
        List<RechargeActivityListVO> rechargeActivityListVOS = iPage.getRecords() ;
        long now = System.currentTimeMillis() ;
        rechargeActivityListVOS.forEach(rechargeActivityListVO -> {
            if(rechargeActivityListVO.getStartTime().compareTo(new Date(now))>0){
                rechargeActivityListVO.setStatus(ActivityStatusEnum.NO_START.getCode());
            }else if(rechargeActivityListVO.getEndTime().compareTo(new Date(now))<0){
                rechargeActivityListVO.setStatus(ActivityStatusEnum.FINASH.getCode());
            }else {
                rechargeActivityListVO.setStatus(ActivityStatusEnum.DOING.getCode());
            }
        });


        return ResultBody.success(iPage) ;
    }

    public ResultBody<List<RechargeActivityListVO>> listByUser(Long userId) {
        User user = userMapper.selectByPrimaryKey(userId) ;
        if(user == null){
            throw new BizException(CommonEnum.USER_NOT_EXIST_ERROR) ;
        }

        List<RechargeActivityListVO> rechargeActivityListVOS = rechargeActivityMapper.selectByUser(userId) ;

        return ResultBody.success(rechargeActivityListVOS) ;
    }
}
